* CONFORMITY AND GROUP PERFORMANCE
* Taher Abofol, Ido Erev and Raanan Sulitzeanu-Kenan
* Human Nature, 2023

use "[PATH...]\Data_Conformity_adaptability.dta"

gen group_num=int(subnum)
order group_num, after(subnum)

*Create a minority dummy (at the individual-level)
gen minority=.
recode minority (.=0) if individualcoice==finaldec
recode minority (.=1) if individualcoice!=finaldec

*Create indicator of individual choice-shift
sort subnum round
by subnum: gen lag_ind_choice = individualcoice[_n-1]
gen shift=.
recode shift(.=1) if individualcoice!=lag_ind_choice
recode shift(.=0) if individualcoice==lag_ind_choice
replace shift=. if round==1

*Create lagged-minority variables
sort subnum round
by subnum: gen lag1_minority= minority[_n-1]

*Create a dummy for the altered phase (change)
gen change=1 if round>60
replace change=0 if round<=60

*Create individual maximization dummy
gen ind_max=.
recode ind_max(.=1) if individualcoice==2 & change==0
recode ind_max(.=0) if individualcoice==2 & change==1
recode ind_max(.=0) if individualcoice==1 & change==0
recode ind_max(.=1) if individualcoice==1 & change==1

*Create individual laged-maximiation vars:
*sort subnum round
*by subnum: gen lag1_max=ind_max[_n-1]

*Create group maximization dummy
gen group_max=.
recode group_max(.=1) if finaldec==2 & change==0
recode group_max(.=0) if finaldec==2 & change==1
recode group_max(.=0) if finaldec==1 & change==0
recode group_max(.=1) if finaldec==1 & change==1

*Create a group-minority dummy
sort round group_num
by round group_num: gen minority2=minority[_n-1]
by round group_num: gen minority3=minority[_n-2]
by round group_num: gen minority4=minority[_n+1]
by round group_num: gen minority5=minority[_n+2]
order minority2, after(minority)
order minority3, after(minority2)
order minority4, after(minority3)
order minority5, after(minority4)
recode minority2 (.=0)
recode minority3 (.=0)
recode minority4 (.=0)
recode minority5 (.=0)
gen group_minority=minority+minority2+minority3+minority4+minority5
order group_minority, after(minority5)

sort subnum round
by subnum: gen lag_group_minority=group_minority[_n-1]

*Create final_payoff variable
gen cond_3=.
recode cond_3 (.=0) if cond!=3
recode cond_3 (.=1) if cond==3
gen final_payoff=payoff+cond_3*group_minority*(1-3*minority)

*Create lagged-final_payoff
sort subnum round
by subnum: gen lag_final_payoff= final_payoff[_n-1]

*Create indications for ten-round sets:
gen round_10=.
recode round_10 (.=10) if round<11
recode round_10 (.=20) if round>10 & round<21
recode round_10 (.=30) if round>20 & round<31
recode round_10 (.=40) if round>30 & round<41
recode round_10 (.=50) if round>40 & round<51
recode round_10 (.=60) if round>50 & round<61
recode round_10 (.=70) if round>60 & round<71
recode round_10 (.=80) if round>70 & round<81
recode round_10 (.=90) if round>80 & round<91
recode round_10 (.=100) if round>90 & round<101


**************************************
*** CONVERSION TO GROUP-LEVEL DATA ***
**************************************
sort group_num round
quietly by group_num round: gen dup=cond(_N==1,0,_n)
drop if dup>1

*Create group laged-maximiation vars:
sort group_num round
by group_num: gen lag1_group_max=group_max[_n-1]
by group_num: gen lag2_group_max=group_max[_n-2]
by group_num: gen lag3_group_max=group_max[_n-3]

order lag_group_minority, after(group_minority)

***************
*** RESULTS ***
***************

*** GROUP-LEVEL ANALYSES ***

* EFFICACY OF THE CONFORMITY TREATMENT (MANIPULATION CHECK)
tabulate cond group_minority if cond>1 & cond<4, chi2 column row


*** TABLE 1 ***
***************
by change, sort : tabulate cond group_max, chi2 column lrchi2 row


*** FIGURE 2 ***
****************
twoway (lpolyci group_max round if cond==1 & round<61) ///
(lpolyci group_max round if cond==2 & round<61) ///
(lpolyci group_max round if cond==3 & round<61) ///
(lpolyci group_max round if cond==1 & round>60) ///
(lpolyci group_max round if cond==2 & round>60) ///
(lpolyci group_max round if cond==3 & round>60)


*** FIGURE A1 ***
*****************
xtgee group_minority i.round_10##i.cond if cond>1, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
margins round_10#cond
marginsplot


* PRELIMINARY ANALYSIS (FOR TABLE 2): IDENTIFYING THE BEST CORRELATION STRUCTURE
qic group_max change round, family(bin) link (logit) corr(independent) i(group_num) t(round) r nolog nodisplay
qic group_max change round, family(bin) link (logit) corr(exchangeable) i(group_num) t(round) r nolog nodisplay
qic group_max change round, family(bin) link (logit) corr(stationary1) i(group_num) t(round) r nolog nodisplay
qic group_max change round, family(bin) link (logit) corr(nonstationary1) i(group_num) t(round) r nolog nodisplay
qic group_max change round, family(bin) link (logit) corr(unstructured) i(group_num) t(round) r nolog nodisplay
* The model with the smallest QIC is "independent".


*** TABLE 2 ***
***************
*MODEL 1: LEARNING & CHANGE
xtgee group_max round i.change if cond<4, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, replace ctitle(Model 1) alpha(0.001, 0.01, 0.05)
*MODEL 2: CONFORMITY AND CHANGE [1-80]
xtgee group_max i.change##ib2.cond round if round<81 & cond<4, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, append ctitle(Model 2) alpha(0.001, 0.01, 0.05)
*MODEL 3: CONFORMITY AND CHANGE [1-90]
xtgee group_max i.change##ib2.cond round if round<91 & cond<4, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, append ctitle(Model 3) alpha(0.001, 0.01, 0.05)
*MODEL 4: CONFORMITY AND CHANGE [1-100]
xtgee group_max i.change##ib2.cond round if cond<4, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, append ctitle(Model 4) alpha(0.001, 0.01, 0.05)


*** FIGURE 3 ***
****************
xtgee group_max i.round_10##ib2.cond if cond<4, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
margins round_10#cond
marginsplot, level (68.2)


*** TABLE 2A ***
****************
*MODEL 1: LEARNING & CHANGE
xtgee group_max round i.change, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, replace ctitle(Model 1) alpha(0.001, 0.01, 0.05)
*MODEL 2: CONFORMITY AND CHANGE [1-80]
xtgee group_max i.change##i.cond round if round<81, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, append ctitle(Model 2) alpha(0.001, 0.01, 0.05)
*MODEL 3: CONFORMITY AND CHANGE [1-90]
xtgee group_max i.change##i.cond round if round<91, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, append ctitle(Model 3) alpha(0.001, 0.01, 0.05)
*MODEL 4: CONFORMITY AND CHANGE [1-100]
xtgee group_max i.change##i.cond round, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
outreg2 using myreg.doc, append ctitle(Model 4) alpha(0.001, 0.01, 0.05)


*** FIGURE A2 ***
*****************
xtgee group_max i.round_10##ib2.cond, family(binomial) link (logit) corr(independent) i(group_num) t(round) robust nolog
margins round_10#cond
marginsplot, level (68.2)


*********************************
*** INDIVIDUAL-LEVEL ANALYSES ***
*********************************
clear
* RUN CODE LINES 5-90 and go to line 205


*** TABLE 3 ***
************************
* MODEL 5 Stable stage:
xtgee shift c.lag_final_payoff##i.lag1_minority##i.cond round if cond>1 & cond<4 & change==0, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	outreg2 using myreg.doc, replace ctitle(Model 1) alpha(0.001, 0.01, 0.05)
* Calculated estimates:
	lincom 1.lag1_minority+1.lag1_minority@3.cond
	lincom lag_final_payoff+lag_final_payoff@3.cond
	lincom lag_final_payoff+lag_final_payoff@1.lag1_minority
	lincom lag_final_payoff+lag_final_payoff@1.lag1_minority+lag_final_payoff@1.lag1_minority@3.cond
	
* MODEL 6 After change:
xtgee shift c.lag_final_payoff##i.lag1_minority##i.cond round if cond>1 & cond<4 & change==1, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	outreg2 using myreg.doc, append ctitle(Model 1) alpha(0.001, 0.01, 0.05)
* Calculated estimates:
	lincom 1.lag1_minority+1.lag1_minority@3.cond
	lincom lag_final_payoff+lag_final_payoff@3.cond
	lincom lag_final_payoff+lag_final_payoff@1.lag1_minority
	lincom lag_final_payoff+lag_final_payoff@1.lag1_minority+lag_final_payoff@1.lag1_minority@3.cond


*** TABLE A1 ***
****************
xtgee shift c.lag_final_payoff##i.lag1_minority##i.cond round if cond>1 & cond<4, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog


*** TABLE A3 ***
****************
* CREATE A CATEGORICAL SOCIAL VARIABLE (0=MINORITY; 1-MAJORITY; 2=UNANIMITY)
gen social=lag_final_payoff
recode social(-7=0)(-5=2)(-4=1)(5=0)(7=2)(8=1)

xtgee shift c.lag_final_payoff##i.lag1_minority##i.change round if cond==2, ///
family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	outreg2 using myreg.doc, replace ctitle(Comparing the two stages) alpha(0.001, 0.01, 0.05)
	
xtgee shift c.lag_final_payoff##i.lag1_minority##i.change round if cond==3, ///
family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	outreg2 using myreg.doc, append ctitle(Comparing the two stages) alpha(0.001, 0.01, 0.05)

	xtgee shift c.lag_final_payoff##ib2.social##i.change round if cond==3, ///
family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	outreg2 using myreg.doc, append ctitle(Comparing the two stages) alpha(0.001, 0.01, 0.05)


*** FIGURE 4 ****
*****************
xtgee shift c.lag_final_payoff round if cond==1 & round<61, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	estimates store i_0to60
xtgee shift c.lag_final_payoff##i.lag1_minority round if cond==2 & round<61, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	estimates store g_0to60
xtgee shift c.lag_final_payoff##i.lag1_minority round if cond==3 & round<61, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	estimates store c_0to60
	
xtgee shift c.lag_final_payoff round if cond==1 & round>60, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	estimates store i_61to100
xtgee shift c.lag_final_payoff##i.lag1_minority round if cond==2 & round>60, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	estimates store g_61to100
xtgee shift c.lag_final_payoff##i.lag1_minority round if cond==3 & round>60, family(binomial) link(logit) corr(independent) i(group_num) t(round) robust nolog
	estimates store c_61to100

	* COEFPLOT (SOCIAL INFO):
	coefplot (g_0to60, label(LC group))(c_0to60, label(HC group)), bylabel(Rounds 0-60) ///
	|| (g_61to100) (c_61to100), bylabel(61-100) ///
	|| , keep(*.lag1_minority) xline(0) ciopts(recast(rcap)) citop

	coefplot (i_0to60, label(Individual group))(g_0to60, label(LC group))(c_0to60, label(HC group)), bylabel(Rounds 0-60) ///
	|| (i_61to100) (g_61to100) (c_61to100), bylabel(61-100) ///
	|| , keep(lag_final_payoff) xline(0) ciopts(recast(rcap)) citop

	* COEFPLOT (ASOCIAL INFO):
	coefplot (i_0to20, label(Individual))(g_0to20, label(LC group))(c_0to20, label(HC group)), bylabel(Rounds 0-20) ///
	|| (i_21to60) (g_21to60) (c_21to60), bylabel(21-60) ///
	|| (i_61to80) (g_61to80) (c_61to80), bylabel(61-80) ///
	|| (i_81to100) (g_81to100) (c_81to100), bylabel(80-100) ///
	|| , keep(*.lag_final_payoff) xline(0) ciopts(recast(rcap)) citop
	

*** FIGURE A3 ***
*****************
logit ind_max i.minority##i.round_10##i.cond if cond>1 & cond<4, cluster(subnum)
margins round_10#minority#cond, level(68)
marginsplot
